<%@ page import="aecs.*" %>
<%@ page import="aecs.test.*" %>
<%@ page import="java.util.concurrent.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>AECS Test Page</title>
</head>
<body>
Welcome, you can test the functionality of AECS below.  For more info visit the 
<a href="http://code.google.com/p/appenginecompletionservice">google code page</a>.

<% 
  String start = request.getParameter("start");
  String end = request.getParameter("end");
  int sum = 0, stubTestSum = 0;
  if (start != null && end != null) {
	try {
		int s = Integer.parseInt(start);
		int e = Integer.parseInt(end);
		int diff = (e-s);
		int splits = (diff < 10 ? diff : 10);
		
		AppEngineCompletionService<Integer> aecs = new AppEngineCompletionService<Integer>();
		int curStart = s;
		for (int i=1;i<=splits;i++) {
			int curEnd = ((e / splits) * i);
			SumCallable callable = new SumCallable(curStart, curEnd);
			aecs.submit(callable);
			curStart = curEnd+1;
		}
	
		int pollCount = 0;
		while (pollCount < splits) {
			java.util.concurrent.Future<Integer> future = aecs.poll(1, TimeUnit.SECONDS);
			if (future != null) {
				sum += future.get();
				pollCount++;
			}
		}
		
		Future<Integer> stubTest = aecs.submit(new SumCallable(s, e));
		stubTestSum = stubTest.get();

	} catch (Exception e) {
		e.printStackTrace(); // get a real stack trace in the logs
		throw e;
	}
%>
   Sum of numbers from <%= start %> to <%= end %> is <%= sum %>, stubTest says: <%= stubTestSum %>
  <br/><br/>
<% } %>
  <form action="index.jsp">
    Start: <input name="start" size="5"/>
    End : <input name="end" size="5"/>
    <input type="submit"/>
  </form>   

</body>

</html>